//
//  HYBookingRoomFilterView.m
//  Booking
//
//  Created by Duntech on 2018/4/19.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYBookingRoomFilterView.h"
#import "HYCityModel.h"
#import "HYBuildingModel.h"

@interface HYBookingRoomFilterView ()

@property (nonatomic,strong) UIView *detailBgView;
@property (nonatomic,strong) UIImageView *searchIconImgView;
@property (nonatomic,strong) UILabel *detailLabel;
@property (nonatomic,strong) UILabel *roomNumberLabel;
@property (nonatomic,strong) UILabel *showAvailiableLabel;
@property (nonatomic,strong) UISwitch *switchView;
@property (nonatomic,strong) UIView *line;
//@property (nonatomic,strong) HYCityModel *cityModel;
//@property (nonatomic,strong) HYBuildingModel *buildingModel;

@end

@implementation HYBookingRoomFilterView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = KAPP_WHITE_COLOR;
        [self setupSubViews];
        [self setupMasonryLayout];
        [self loadCityData];
    }
    return self;
}

- (instancetype)initWithFilterViewMode:(FilterViewMode)mode{
    
    self = [super init];
    if (self) {
        
        self.filterViewMode = mode;
        self.backgroundColor = KAPP_WHITE_COLOR;
        [self setupSubViews];
        [self setupMasonryLayout];
        [self loadCityData];
    }
    return self;
}

- (void)setupSubViews{
    
    [self addSubview:self.detailBgView];
    [self addSubview:self.searchIconImgView];
    [self addSubview:self.detailLabel];
    [self addSubview:self.roomNumberLabel];
    [self addSubview:self.showAvailiableLabel];
    [self addSubview:self.switchView];
    [self addSubview:self.line];
}

- (void)setupMasonryLayout{
    
    [_detailBgView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.top.right.equalTo(self);
        make.height.mas_equalTo(KTopFilterViewHeight);
    }];
    
    [_searchIconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self).offset(12 * WIDTH_MULTIPLE);
        make.left.equalTo(self).offset(10 * WIDTH_MULTIPLE);
        make.height.mas_equalTo(15 * WIDTH_MULTIPLE);
    }];
    
    [_detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self.detailBgView);
        make.right.equalTo(self).offset(-10 * WIDTH_MULTIPLE);
        make.left.equalTo(self.searchIconImgView.mas_right).offset(10 * WIDTH_MULTIPLE);
    }];
    
    [_roomNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
        make.left.equalTo(self).offset(10 * WIDTH_MULTIPLE);
        make.top.equalTo(self.detailBgView.mas_bottom);
        make.width.mas_equalTo(120);
    }];
    
    [_switchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-10 * WIDTH_MULTIPLE);
        make.centerY.equalTo(self.roomNumberLabel);
    }];
    
    [_showAvailiableLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.switchView.mas_left).offset(-20);
        make.top.bottom.equalTo(self.roomNumberLabel);
    }];
    
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(WIDTH_MULTIPLE);
    }];
}

-(void)setHideSwitch{
    self.roomNumberLabel.hidden = YES;
    self.showAvailiableLabel.hidden = YES;
    self.switchView.hidden = YES;
}

- (void)loadCityData{
    
    if (self.filterViewMode == FilterViewModePlane) {
        
        kWeakSelf(weakSelf);
        [HYNetRequestHandle getCityListComplection:^(NSArray *resultList) {
            
            if (!resultList) return;
            NSMutableArray *cityBuildsMarr = [NSMutableArray new];
            for (NSDictionary *dic in resultList) {
                HYCityModel *model =  [HYCityModel modelWithDictionary:dic];
                [cityBuildsMarr addObject:model];
            }
            HYCityModel *cityModel = cityBuildsMarr.count == 0 ? [HYCityModel new] : cityBuildsMarr[0];
            HYBuildModel *buildModel = cityModel.builds.count == 0 ? [HYBuildModel new] : cityModel.builds[0];
            
            NSString *dateStr = [HYDateHandle getCurrentDateStrDDMMYYYYWithTimeInterval:0];
            NSString *startTime = [HYDateHandle getCurrentDateStrHHmmWithTimeInterval:0];
            NSString *endTime = [HYDateHandle getCurrentDateStrHHmmWithTimeInterval:1 * 60 * 60];
            weakSelf.detailLabel.text = [NSString stringWithFormat:@"%@ %@ 1F %@ %@ ~ %@",cityModel.name ? cityModel.name : @"",buildModel.name ? buildModel.name : @"",dateStr,startTime,endTime];
            if (weakSelf.initSearchBlock) {
                weakSelf.initSearchBlock([NSDate date], startTime, endTime, cityBuildsMarr);
            }
            
        }];
    }
    else{
        
        kWeakSelf(weakSelf);
        [HYNetRequestHandle getCityListComplection:^(NSArray *resultList) {
            if (!resultList) return;
            NSMutableArray *cityBuildsMarr = [NSMutableArray new];
            for (NSDictionary *dic in resultList) {
                HYCityModel *model =  [HYCityModel modelWithDictionary:dic];
                [cityBuildsMarr addObject:model];
            }
            HYCityModel *cityModel = cityBuildsMarr.count == 0 ? [HYCityModel new] : cityBuildsMarr[0];
            HYBuildModel *buildModel = cityModel.builds.count == 0 ? [HYBuildModel new] : cityModel.builds[0];
            
            NSString *dateStr = [HYDateHandle getCurrentDateStrDDMMYYYYWithTimeInterval:0];
            NSString *startTime = [HYDateHandle getCurrentDateStrHHmmWithTimeInterval:0];
            NSString *endTime = [HYDateHandle getCurrentDateStrHHmmWithTimeInterval:1 * 60 * 60];
            weakSelf.detailLabel.text = [NSString stringWithFormat:@"%@ %@  %@ %@ ~ %@",cityModel.name ? cityModel.name : @"",buildModel.name ? buildModel.name : @"",dateStr,startTime,endTime];
            if (weakSelf.initSearchBlock) {
                weakSelf.initSearchBlock([NSDate date], startTime, endTime, cityBuildsMarr);
            }
            
        }];
    }
    
    
}

#pragma mark - setter
- (void)setRoomNumber:(NSString *)roomNumber{
    
    _roomNumber = roomNumber;
    self.roomNumberLabel.text = [NSString stringWithFormat:@"%@ %@",roomNumber,NSLocalizedString(@"roomUnit", nil)];
}

- (void)setDateStr:(NSString *)dateStr{
    
    _dateStr = dateStr;
    self.detailLabel.text = dateStr;
}

- (void)switchAction:(UISwitch *)sender{
    
    if (self.SwitchActionBlock) {
        
        self.SwitchActionBlock(sender.isOn);
    }
}

#pragma mark - lazyload
- (UIView *)detailBgView{
    
    if (!_detailBgView) {
        _detailBgView = [UIView new];
        _detailBgView.backgroundColor = KAPP_FILTER_TOP_COLOR;
    }
    return _detailBgView;
}

- (UIImageView *)searchIconImgView{
    
    if (!_searchIconImgView) {
        
        _searchIconImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
        _searchIconImgView.contentMode = UIViewContentModeScaleAspectFit;
        _searchIconImgView.image = [UIImage imageNamed:@"search"];
        _searchIconImgView.clipsToBounds = YES;
    }
    return _searchIconImgView;
}

- (UILabel *)detailLabel{
    
    if (!_detailLabel) {
        _detailLabel = [UILabel new];
        _detailLabel.textColor = KAPP_WHITE_COLOR;
        _detailLabel.font = KFitFont(14);
        _detailLabel.textAlignment = NSTextAlignmentCenter;
       
    }
    return _detailLabel;
}

- (UILabel *)roomNumberLabel{
    
    if (!_roomNumberLabel) {
        _roomNumberLabel = [UILabel new];
        _roomNumberLabel.textColor = KAPP_BLACK_COLOR;
        _roomNumberLabel.font = KFitFont(14);
        _roomNumberLabel.text = NSLocalizedString(@"roomUnit", nil);
    }
    return _roomNumberLabel;
}

- (UILabel *)showAvailiableLabel{
    
    if (!_showAvailiableLabel) {
        _showAvailiableLabel = [UILabel new];
        _showAvailiableLabel.textColor = KAPP_BLACK_COLOR;
        _showAvailiableLabel.font = KFitFont(14);
        _showAvailiableLabel.text = NSLocalizedString(@"showAvailable", nil);
        _showAvailiableLabel.textAlignment = NSTextAlignmentRight;
    }
    return _showAvailiableLabel;
}

- (UISwitch *)switchView{
    
    if (!_switchView) {
        
        _switchView = [[UISwitch alloc] init];
        _switchView.onTintColor = KAPP_NAV_TITLE_COLOR;
        _switchView.tintColor = KAPP_NAV_TITLE_COLOR;
        [_switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _switchView;
}

- (UIView *)line{
    
    if (!_line) {
        _line = [UIView new];
        _line.backgroundColor = KCOLOR(@"808080");
    }
    return _line;
}


@end
